package com.itheima.reggie.contronller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersContronller {
    @Autowired
    private OrdersService ordersService;


    /**
     * 用户下单功能实现
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> addWithOrderDetail(@RequestBody Orders orders) {
        ordersService.addWithOrderDetail(orders);

        return R.success("下单成功");

    }


    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        log.info("beginTime:{},endTime:{}", beginTime, endTime);
        Page<Orders> ordersPage = ordersService.pageOrders(page, pageSize, number, beginTime, endTime);

        return R.success(ordersPage);
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {
        Page<OrdersDto> userOrderPage = ordersService.userOrderPage(page, pageSize);
        return R.success(userOrderPage);
    }


    @PutMapping
    public R<String> paisong(@RequestBody Orders orders){
        log.info(orders.toString());
        ordersService.updateById(orders);
        return R.success("派送成功");
    }
}
